package com.sctu.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sctu.EMSApp.PlayHotelMoreListActivity;
import com.sctu.EMSApp.PlayHotelOneActivity;
import com.sctu.EMSApp.R;
import com.sctu.bean.PlayHotelListBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 鑫爷您慢点 on 2015/12/16.
 */
public class PlayHotelListViewAdapter extends BaseAdapter{
    private List<PlayHotelListBean.Results> list = new ArrayList<PlayHotelListBean.Results>();
    private Activity activity;
    private ViewHolder viewHolder;

    public PlayHotelListViewAdapter(Activity activity, List<PlayHotelListBean.Results> list) {
        this.activity = activity;
        this.list = list;
    }

    public PlayHotelListViewAdapter() {

    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        if(convertView == null){//优化listView
            view = LayoutInflater.from(activity).inflate(R.layout.item_play_hotel_list_view, null);
            viewHolder = new ViewHolder();
            viewHolder.setTextTitle((TextView) view.findViewById(R.id.item_play_hotel_list_view_top_text));
            viewHolder.setTextChildTitleOne((TextView) view.findViewById(R.id.item_play_hotel_one_text));
            viewHolder.setTextChildTitleTwo((TextView) view.findViewById(R.id.item_play_hotel_two_text));
            viewHolder.setTextChildTitleThree((TextView) view.findViewById(R.id.item_play_hotel_three_text));
            viewHolder.setTextMore((TextView) view.findViewById(R.id.item_play_hotel_text_more));

            ImageView[] imageViews = viewHolder.getImageViews();
            imageViews[0] = (ImageView) view.findViewById(R.id.item_play_hotel_one_img);
            imageViews[1] = (ImageView) view.findViewById(R.id.item_play_hotel_two_img);
            imageViews[2] = (ImageView) view.findViewById(R.id.item_play_hotel_three_img);
            viewHolder.setImageViews(imageViews);
            view.setTag(viewHolder);//将viewHolder储存在view中
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        for (int i = 0; i < list.get(position).getArticleList().size(); i++) {
            String path = list.get(position).getArticleList().get(i).getPath();
            GetVolleyHttp.getInstance(activity).setImageLoader(viewHolder.getImageViews()[i],
                    R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + path);

            final int finalI = i;
            viewHolder.getImageViews()[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转PlayHotelOneActivity
                    String id = String.valueOf(list.get(position).getArticleList().get(finalI).getId());
                    PlayHotelOneActivity.actionStart(activity,id);
                }
            });
        }
        viewHolder.getTextTitle().setText(list.get(position).getName());
        //设置文字信息
        for (int j = 0; j < list.get(position).getArticleList().size(); j++) {
            switch (j) {
                case 0:
                    viewHolder.getTextChildTitleOne().setText(list.get(position).getArticleList().get(0).getTitle());
                    break;
                case 1:
                    viewHolder.getTextChildTitleTwo().setText(list.get(position).getArticleList().get(1).getTitle());
                    break;
                case 2:
                    viewHolder.getTextChildTitleThree().setText(list.get(position).getArticleList().get(2).getTitle());
                    break;
                default:
                    break;
            }
        }
        viewHolder.getTextMore().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转PlayHotelMoreListActivity
                PlayHotelMoreListActivity.actionStart(list.get(position).getUuid(), activity);
            }
        });

        return view;
    }

    /**
     * listView 优化内部类
     */
    private class ViewHolder{
        private TextView textTitle, textChildTitleOne, textChildTitleTwo, textChildTitleThree,textMore;
        private ImageView[] imageViews = new ImageView[3];

        public TextView getTextTitle() {
            return textTitle;
        }

        public void setTextTitle(TextView textTitle) {
            this.textTitle = textTitle;
        }

        public TextView getTextChildTitleOne() {
            return textChildTitleOne;
        }

        public void setTextChildTitleOne(TextView textChildTitleOne) {
            this.textChildTitleOne = textChildTitleOne;
        }

        public TextView getTextChildTitleTwo() {
            return textChildTitleTwo;
        }

        public void setTextChildTitleTwo(TextView textChildTitleTwo) {
            this.textChildTitleTwo = textChildTitleTwo;
        }

        public TextView getTextChildTitleThree() {
            return textChildTitleThree;
        }

        public void setTextChildTitleThree(TextView textChildTitleThree) {
            this.textChildTitleThree = textChildTitleThree;
        }

        public TextView getTextMore() {
            return textMore;
        }

        public void setTextMore(TextView textMore) {
            this.textMore = textMore;
        }

        public ImageView[] getImageViews() {
            return imageViews;
        }

        public void setImageViews(ImageView[] imageViews) {
            this.imageViews = imageViews;
        }
    }
}
